﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Maps.MapControl;

namespace ODAF.SilverlightApp
{
    public enum ViewState
    {
        AnimatingState,
        InfoViewState,
        CommentViewState,
        RatingViewState,
        TagViewState
    }

    public class PinWheelViewStateChangeEventArgs : EventArgs
    {
        public ViewState viewState;

        public PinWheelViewStateChangeEventArgs(ViewState currentState)
        {
            this.viewState = currentState;
        }
    }

    public delegate void PinWheelViewStateChangeEventHandler(object sender, PinWheelViewStateChangeEventArgs e);


    public partial class PinWheel : UserControl
    {

        public event PinWheelViewStateChangeEventHandler ViewStateChange;

        private Storyboard storyBoard;
        private ViewState viewState;

        private double currentRotVal = 0.0;

        public PinWheel()
        {
            viewState = ViewState.InfoViewState;

            InitializeComponent();

            slider.ValueChanged += new RoutedPropertyChangedEventHandler<double>(slider_ValueChanged);

            

            imgStar.MouseLeftButtonUp += new MouseButtonEventHandler(imgStar_MouseLeftButtonUp);
            imgStar.MouseEnter += new MouseEventHandler(imgStar_MouseEnter);
            imgStar.MouseLeave += new MouseEventHandler(imgStar_MouseLeave);

            imgComment.MouseLeftButtonUp += new MouseButtonEventHandler(imgComment_MouseLeftButtonUp);
            imgComment.MouseEnter += new MouseEventHandler(imgComment_MouseEnter);
            imgComment.MouseLeave += new MouseEventHandler(imgComment_MouseLeave);

            imgTag.MouseLeftButtonUp +=new MouseButtonEventHandler(imgTag_MouseLeftButtonUp);
            imgTag.MouseEnter += new MouseEventHandler(imgTag_MouseEnter);
            imgTag.MouseLeave += new MouseEventHandler(imgTag_MouseLeave);


            imgInfo.MouseLeftButtonUp += new MouseButtonEventHandler(imgInfo_MouseLeftButtonUp);
            imgInfo.MouseEnter += new MouseEventHandler(imgInfo_MouseEnter);
            imgInfo.MouseLeave += new MouseEventHandler(imgInfo_MouseLeave);

            storyBoard = new Storyboard();
            storyBoard.Completed += new EventHandler(storyBoard_Completed);
            
        }

        void imgStar_MouseLeave(object sender, MouseEventArgs e)
        {
            upStarImg.Visibility = Visibility.Visible;
            hoverStarImg.Visibility = Visibility.Collapsed;
        }

        void imgStar_MouseEnter(object sender, MouseEventArgs e)
        {
            hoverStarImg.Visibility = Visibility.Visible;
            upStarImg.Visibility = Visibility.Collapsed;
        }

        void imgComment_MouseLeave(object sender, MouseEventArgs e)
        {
            upCommentImg.Visibility = Visibility.Visible;
            hoverCommentImg.Visibility = Visibility.Collapsed;
        }

        void imgComment_MouseEnter(object sender, MouseEventArgs e)
        {
            hoverCommentImg.Visibility = Visibility.Visible;
            upCommentImg.Visibility = Visibility.Collapsed;
        }

        void imgTag_MouseLeave(object sender, MouseEventArgs e)
        {
            upTagImg.Visibility = Visibility.Visible;
            hoverTagImg.Visibility = Visibility.Collapsed;
        }

        void imgTag_MouseEnter(object sender, MouseEventArgs e)
        {
            hoverTagImg.Visibility = Visibility.Visible;
            upTagImg.Visibility = Visibility.Collapsed;
        }

        void imgInfo_MouseLeave(object sender, MouseEventArgs e)
        {
            upInfoImg.Visibility = Visibility.Visible;
            hoverInfoImg.Visibility = Visibility.Collapsed;
        }

        void imgInfo_MouseEnter(object sender, MouseEventArgs e)
        {
            hoverInfoImg.Visibility = Visibility.Visible;
            upInfoImg.Visibility = Visibility.Collapsed;
        }

        

        void imgInfo_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (viewState != ViewState.InfoViewState)
            {
                viewState = ViewState.InfoViewState;
                rotateTo(0);
            }
        }

        void imgTag_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (viewState != ViewState.TagViewState)
            {
                viewState = ViewState.TagViewState;
                rotateTo(-90);
            }                
        }

        void imgStar_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (viewState != ViewState.RatingViewState)
            {
                viewState = ViewState.RatingViewState;
                rotateTo(90);
            }
        }

        void imgComment_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (viewState != ViewState.CommentViewState)
            {
                viewState = ViewState.CommentViewState;
                if (currentRotVal < 0)
                {
                    rotateTo(-180);
                }
                else
                {
                    rotateTo(180);
                }
            }
        }

        void rotateTo(int degrees)
        {
            DispatchAnimatingState();
            storyBoard.Stop();
            storyBoard.Children.Clear();
            if (currentRotVal == 180 && degrees < 0)
            {
                currentRotVal = -180;
            }
            else if (currentRotVal == -180 && degrees > 0)
            {
                currentRotVal = 180;
            }

            DoubleAnimation anim = new DoubleAnimation();
            anim.From = currentRotVal;
            anim.To = currentRotVal = degrees;
            anim.Duration = TimeSpan.FromSeconds(0.6);
            Storyboard.SetTarget(anim, slider);
            Storyboard.SetTargetProperty(anim, new PropertyPath("Value"));
            storyBoard.Children.Add(anim);
            
            storyBoard.Begin();
        }

        void storyBoard_Completed(object sender, EventArgs e)
        {
            // dispatch some sort of event for ViewState changed
            PinWheelViewStateChangeEventArgs args = new PinWheelViewStateChangeEventArgs(this.viewState);
            if (ViewStateChange != null)
            {
                ViewStateChange(this, args);
            }
            
        }

        void DispatchAnimatingState()
        {
            PinWheelViewStateChangeEventArgs args = new PinWheelViewStateChangeEventArgs(ViewState.AnimatingState);
            ViewStateChange(this, args);
        }

        void slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            canvasRot.Angle = slider.Value;

            RotateTransform rot = new RotateTransform();
            rot.CenterX = 8;
            rot.CenterY = 8;
            rot.Angle = -canvasRot.Angle;
            imgComment.RenderTransform = rot;
            imgTag.RenderTransform = rot;
            imgInfo.RenderTransform = rot;
            imgStar.RenderTransform = rot;
        }

        

       
    }
}
